﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GanagroLiteBO;
using GeneralUtilities;

namespace GanagroLite
{
    public partial class FormEditarSemoviente : WinPart
    {
        GANADO_BO currentGanado;
        private bool _modoConsulta = false;

        #region constructors

        public FormEditarSemoviente(bool modoConsulta)
        {
            InitializeComponent();
            _modoConsulta = modoConsulta;
            this.buttonAceptar.Enabled = !_modoConsulta;
        }

        #endregion

        #region Internal methods
        protected internal override object GetIdValue()
        {
            return "Editar / Consultar Semovientes" ;
        }

        public override string ToString()
        {
            return  string.Format("{0} Semovientes", _modoConsulta ? "Consultar" : "Editar");
        }

        #endregion

        #region UI Event handlers

        private void FormEditarSemoviente_Load(object sender, EventArgs e)
        {

            this.ComboBoxColor.ValueMember = "ID";
            this.ComboBoxColor.DisplayMember = "DESCRIPCION";

            this.ComboBoxEstado.ValueMember = "ID";
            this.ComboBoxEstado.DisplayMember = "DESCRIPCION";

            this.ComboBoxRaza1.ValueMember = "ID";
            this.ComboBoxRaza1.DisplayMember = "DESCRIPCION";

            this.ComboBoxRaza2.ValueMember = "ID";
            this.ComboBoxRaza2.DisplayMember = "DESCRIPCION";

            this.ComboBoxLote.ValueMember = "ID";
            this.ComboBoxLote.DisplayMember = "DESCRIPCION";

            this.GridViewMain.AutoGenerateColumns = true;

            this.TextBoxCodigo.Focus();

            this.backgroundWorker1.WorkerReportsProgress = false;
            this.backgroundWorker1.RunWorkerAsync();

        }

        private void TextBoxCodigo_Enter(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TextBoxCodigo.Text))
            {
                ClearControlsGanado();
            }

        }

        private void TextBoxCodigo_Validating(object sender, CancelEventArgs e)
        {
            currentGanado.CODIGO = TextBoxCodigo.Text;
            errorProviderSemovientes.SetError(TextBoxCodigo, string.Empty);

            if (currentGanado.Isvalid() == false)
            {
                errorProviderSemovientes.SetError(TextBoxCodigo, currentGanado.Error);
                e.Cancel = true;
            }
        }

        private async void CodigoTextBox_Validated(object sender, EventArgs e)
        {
            try
            {

                if ((TextBoxCodigo.Text.Trim() != string.Empty))
                {
                    this.BindingSourceGanadoBO.SuspendBinding();
                    currentGanado.Load(TextBoxCodigo.Text);
                    this.bindingSourcePesajes.DataSource = currentGanado.PesajesBO;
                    this.labelInactivo.Visible = (currentGanado.CODIGO != null && currentGanado.Activo == 0);
                    this.BindingSourceGanadoBO.ResumeBinding();
                    this.BindingSourceGanadoBO.ResetBindings(false);

                    int intResult = await currentGanado.GetGananciaVida();
                    this.labelGananciaVida.Text = intResult.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ClearControlsGanado()
        {
            this.ComboBoxColor.SelectedValue = 0;
            this.ComboBoxEstado.SelectedValue = 10;
            this.ComboBoxLote.SelectedValue = 5;
            this.ComboBoxRaza1.SelectedValue = 0;
            this.ComboBoxRaza2.SelectedValue = 0;
            this.TextBoxSexo.Text = string.Empty;
            this.ComentarioTextBox.Text = string.Empty;
            this.MarcaTextBox.Text = string.Empty;
            this.bindingSourcePesajes.DataSource = null;
            this.labelInactivo.Visible = false;
            dateTimePickerEntrada.Value = Convert.ToDateTime("1900-01-01");
            dateTimePickerSalida.Value = Convert.ToDateTime("1900-01-01");
            MainForm.Instance.StatusLabel1.Text = string.Empty;
        }

        private void buttonAceptar_Click(object sender, EventArgs e)
        {
            if (currentGanado.CODIGO != null && currentGanado.Isvalid(true))
            {
                if (_modoConsulta == false)
                {
                    bool success = false;
                    if (currentGanado.Guardar(ref success))
                    {
                        MainForm.Instance.StatusLabel1.Text = string.Format("Los datos de {0} han sido actualizados", currentGanado.CODIGO);
                    }
                }
            }
        }

        private void buttonSalir_Click(object sender, EventArgs e)
        {
            this.BindingSourceGanadoBO.CancelEdit();
            this.bindingSourceColores.CancelEdit();
            this.bindingSourceEstados.CancelEdit();
            this.bindingSourceLotes.CancelEdit();
            this.bindingSourcePesajes.CancelEdit();
            this.bindingSourceRaza1.CancelEdit();
            this.bindingSourceRaza2.CancelEdit();
            this.Close();
        }

        private void TextBoxSexo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar != (char)Keys.M) && (e.KeyChar != (char)Keys.H))
            {
                // Consume this invalid key and beep
                e.Handled = true;
            }

        }

        #endregion

        #region backgorundWorker
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            currentGanado = new GANADO_BO();
            this.BindingSourceGanadoBO.DataSource = currentGanado;
            this.bindingSourceColores.DataSource = UtilityDB.Loadcolores(); //TODO: Replace with PresenterLookup.LoadColores
            this.bindingSourceEstados.DataSource = UtilityDB.Loadestados();
            this.bindingSourceRaza1.DataSource = UtilityDB.Loadrazas();
            this.bindingSourceRaza2.DataSource = UtilityDB.Loadrazas();
            this.bindingSourceLotes.DataSource = UtilityDB.Loadlotes();
       }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.ComboBoxColor.DataSource = bindingSourceColores;
            this.ComboBoxEstado.DataSource = bindingSourceEstados;
            this.ComboBoxRaza1.DataSource = bindingSourceRaza1;
            this.ComboBoxRaza2.DataSource = bindingSourceRaza2;
            this.ComboBoxLote.DataSource = bindingSourceLotes;
            ClearControlsGanado();
        }
        #endregion


    }
}
